exports.__esModule = true;

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

/**
 * Created by Jessi on 2017/11/30.
 */
exports.default = class extends think.Service {
  constructor() {
    super();
    this.partsGroupModel = this.db['cms_parts_group'];
  }
  /**
     * 查询全部部件分类列表
     * @return
     * aj --Object
     * aj.result --部件列表 Array
     */
  getPratsGroupList() {
    var _this = this;

    return _asyncToGenerator(function* () {
      const serverIP = 'http://' + think.config('staticIp') + ':' + think.config('proxyPort');
      const defaultImg = serverIP + '/static/upload/partIcons/default.svg';
      const aj = think.ajaxJson();
      const attr = {
        exclude: ['del_flag']
      };
      const where = {
        del_flag: 0
      };
      const PartsGroupList = yield _this.partsGroupModel.findAll({ where: where, attributes: attr });
      if (PartsGroupList.error) {
        aj.success = false;
        aj.msg = '数据库查询错误';
      } else {
        PartsGroupList.map(function (item) {
          if (item.group_pic_path) {
            item.group_pic_path = serverIP + '/' + item.group_pic_path;
          } else {
            item.group_pic_path = defaultImg;
          }
          return item;
        });
        aj.result = PartsGroupList;
      }
      return aj;
    })();
  }
  getPratsGroupForParent(data) {
    var _this2 = this;

    return _asyncToGenerator(function* () {
      const db = _this2.db;
      const attributes = ['id', 'group_code', 'group_name', 'parent_id'];
      try {
        const res = yield db['cms_parts_group'].findAll({
          where: {
            del_flag: 0,
            parent_id: data.parent_id
          }
        });
        return res;
      } catch (err) {
        return _this2.exceptionService.handleError(err);
      }
    })();
  }

  /** ********************************************** */

  /**
     * 分页查询部件分类列表
     * @data
     * data.page --页码
     * data.pageSize --单页条数
     * @return
     * return.count --总记录数
     * return.rows -- 列表
     */
  getPartsGroupList(data) {
    var _this3 = this;

    return _asyncToGenerator(function* () {
      data.start = (parseInt(data.page) - 1) * parseInt(data.pageSize);
      const where = {
        del_flag: 0
      };
      const Op = _this3.db.Sequelize.Op;
      if (data.group_name) {
        where.group_name = {
          [Op.like]: '%' + data.group_name + '%'
        };
      }
      if (data.id) {
        where[Op.or] = {
          id: data.id,
          parent_id: data.id
        };
      }
      try {
        return yield _this3.partsGroupModel.findAndCountAll({
          where: where,
          limit: parseInt(data.pageSize),
          offset: parseInt(data.start)
        });
      } catch (err) {
        return _this3.exceptionService.handleError(err);
      }
    })();
  }

  /**
     * 检查部件分类代码和分类名称是否重复
     */
  checkCode(data) {
    var _this4 = this;

    return _asyncToGenerator(function* () {
      const Op = _this4.db.Sequelize.Op;
      const where = {
        [Op.or]: [{
          group_name: data.group_name
        }, {
          group_code: data.group_code
        }]
      };
      if (data.id) {
        where.id = {
          [Op.not]: data.id
        };
      }
      try {
        return yield _this4.partsGroupModel.count({
          where: where
        });
      } catch (err) {
        return _this4.exceptionService.handleError(err);
      }
    })();
  }

  /**
     * 保存部件分类信息
     */
  savePGInfo(data) {
    var _this5 = this;

    return _asyncToGenerator(function* () {
      try {
        if (data.id) {
          return yield _this5.partsGroupModel.update(data, {
            where: {
              id: data.id
            }
          });
        } else {
          return yield _this5.partsGroupModel.create(data);
        }
      } catch (err) {
        return _this5.exceptionService.handleError(err);
      }
    })();
  }

  /**
     * 删除部件分类，逻辑删除
     */
  delPGInfo(data) {
    var _this6 = this;

    return _asyncToGenerator(function* () {
      try {
        const counts = yield _this6.partsGroupModel.count({
          where: {
            parent_id: data.id,
            del_flag: 0
          }
        });
        if (counts > 0) {
          return {
            msg: '当前部件分类包含子类，请先删除子类'
          };
        } else {
          // return await this.partsGroupModel.update({del_flag:1},{where:{id:data.id}});
          return yield _this6.partsGroupModel.destroy({
            where: {
              id: data.id
            }
          });
        }
      } catch (err) {
        return _this6.exceptionService.handleError(err);
      }
    })();
  }
  /** ************************************* */
};